package de.wz.schwimmen;

import static de.wz.schwimmen.Config.NUM_PLAYER;
import static de.wz.schwimmen.StaticDataStorage.EDIT_TEXT_PAYMENT;

import java.io.PrintWriter;
import java.io.StringWriter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author Peter Eimann
 */
public class PaymentActivity extends Activity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable exception) {
                StringWriter stackTrace = new StringWriter();
                exception.printStackTrace(new PrintWriter(stackTrace));

                StringBuilder errormessage = new StringBuilder(exception.getMessage());
            	while (exception.getCause() != null) {
            		exception = exception.getCause();
            		errormessage.append("\n\t\t- ").append(exception.getMessage());
            	}

                Intent spielIntent = new Intent(PaymentActivity.this, SpielActivity.class);
                spielIntent.putExtra("de.wz.schwimmen.errormessage", errormessage.toString());
                spielIntent.putExtra("de.wz.schwimmen.stacktrace", stackTrace.toString());
                spielIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                spielIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(spielIntent);
                System.exit(0);
            }
        });

		setContentView(R.layout.payment);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	@Override
	public void onStart() {
		super.onStart();

		Button buttonOk = findViewById(R.id.buttonPaymentOk, Button.class);
		buttonOk.setOnClickListener(this);

		Bundle basket = getIntent().getExtras();
		for (int player = 0; player < NUM_PLAYER; player++) {
			findViewById(EDIT_TEXT_PAYMENT[player], EditText.class).setHint(getResources().getString(
				R.string.text_topay, basket.getString("amount_player" + (player + 1))));
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.buttonPaymentOk:
				handleButtonOk();
				break;
		}
	}

	private void handleButtonOk() {
		for (int player = 0; player < NUM_PLAYER; player++) {
			if (TextUtils.isEmpty(findViewById(EDIT_TEXT_PAYMENT[player], EditText.class).getText().toString())) {
				Toast.makeText(this, getResources().getString(R.string.text_checkpayments), Toast.LENGTH_LONG).show();
				return;
			}
		}

		Intent data = new Intent();
		Bundle basket = new Bundle();
		for (int player = 0; player < NUM_PLAYER; player++) {
			basket.putString("payment_player" + (player + 1), 
				findViewById(EDIT_TEXT_PAYMENT[player], EditText.class).getText().toString());
		}
		data.putExtras(basket);

		setResult(RESULT_OK, data);
		finish();
	}

	@SuppressWarnings("unchecked")
	private <T extends View> T findViewById(int id, Class<T> clazz) {
		return (T)findViewById(id);
	}
} 